package com.ak.cluster;

import java.io.IOException;

import com.ak.player.Player;
import com.ak.renderer.WorldRenderer;
import com.ak.ui.ClusterUI;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.tablelayout.Table;
import com.badlogic.gdx.scenes.scene2d.ui.tablelayout.TableLayout;

public class GameScreen extends Screen implements InputProcessor{
	public Game mGame; 
	public OrthographicCamera gameCamera; 
	public InputProcessor gameInput;
	public WorldRenderer wRender;
	public BitmapFont debugFont;
	public ClusterUI cUI; 
	private Stage ui;
	public GameWorld world;
	private Table gameTable;
	boolean isPressed;
	int[] touchPosition = new int[2];
	public Player mPlayer;
	public AssetManager aM;
	public ClusterUI mainUI;
	public float AspectRatio;
	
	
	public GameScreen(Game lgame) {
		
		super(lgame);
		isPressed = false;
		aM = new AssetManager();
		mGame = lgame; 
		AspectRatio = Gdx.graphics.getWidth()/Gdx.graphics.getHeight();
		gameCamera = new OrthographicCamera(48/AspectRatio,32/AspectRatio);
		gameCamera.position.set(0,16,0);
		mGame.setRatioCamera(gameCamera);
		//needs to come after the camera because it utilizes camera information.
		
		//Renderer
		world = new GameWorld(gameCamera);
		aM.load(world);
		wRender = new WorldRenderer(world);
		world.createWorld();
		
		ui = new Stage(Gdx.graphics.getWidth(),Gdx.graphics.getHeight(),false);
		Gdx.input.setInputProcessor(ui);
		mPlayer = new Player();
		
		gameTable = new Table();
		ui.addActor(gameTable);
		gameTable.width = Gdx.graphics.getWidth();
		gameTable.height = Gdx.graphics.getHeight();
		gameTable.x = gameTable.y = 0;
		cUI = new ClusterUI(gameTable,ui, this);  //technically pointing to clusterui address
		
	}
	

	@Override
	public void update(float deltatime) {
		// TODO Auto-generated method stub

		world.update(deltatime);
	}

	@Override
	public void present(float deltatime) {		
	    Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
	    Gdx.graphics.getGL10().glEnable(GL10.GL_TEXTURE_2D);
		//DRAW ORDER MATTERS HERE!
        if(Gdx.input.isTouched(0)&&isPressed!=true)
        {
        	isPressed = true;
        	//Use Delta!~~
        	touchPosition[0] = Gdx.input.getX(0);
        	touchPosition[1] = Gdx.input.getY(0);
        }
        //Works beautifully!
        //This is the screen gesture movement.
        if(isPressed == true)
        {
        	if(Gdx.input.getX(0) != touchPosition[0] || Gdx.input.getY()!=touchPosition[1])
        	{
        		float tX;
        		float tY;
        		tX = -1*(Gdx.input.getDeltaX(0)*0.05f);
        		tY = (Gdx.input.getDeltaY(0)*0.05f);
        		cameraCheck();
        		gameCamera.translate(tX,tY, 0);
        	}
        }
        if(!Gdx.input.isTouched(0))
        {
        	isPressed = false;
        	cameraCheck();
        }
        
		wRender.render();
		//cUI.updateText();
        ui.act(Math.min(Gdx.graphics.getDeltaTime(), 1 / 30f));
		ui.draw();
		Table.drawDebug(ui);

		
	}
	public void cameraCheck()
	{
		if(gameCamera.position.x >= AssetIndex.WORLD_WIDTH)
			gameCamera.translate(-2*0.1f,0,0);
		if(gameCamera.position.x <= -1*(AssetIndex.WORLD_WIDTH))
			gameCamera.translate(2*0.1f, 0, 0);
		if(gameCamera.position.y >= AssetIndex.WORLD_TOP)
			gameCamera.translate(0,-2*0.1f,0);
		if(gameCamera.position.y <= -1*(AssetIndex.WORLD_BOTTOM))
			gameCamera.translate(0, 2*0.1f, 0);
	}
	@Override
	public void pause() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub
		wRender.dispose();
		world.dispose();
		
	}


	@Override
	public boolean keyDown(int keycode) {
		// TODO Auto-generated method stub
		return false;
	}


	@Override
	public boolean keyUp(int keycode) {
		// TODO Auto-generated method stub
		return false;
	}


	@Override
	public boolean keyTyped(char character) {
		// TODO Auto-generated method stub
		return false;
	}


	@Override
	public boolean touchDown(int x, int y, int pointer, int button) {
		// TODO Auto-generated method stub
		return false;
	}


	@Override
	public boolean touchUp(int x, int y, int pointer, int button) {
		// TODO Auto-generated method stub
		return false;
	}


	@Override
	public boolean touchDragged(int x, int y, int pointer) {
		// TODO Auto-generated method stub
		return false;
	}


	@Override
	public boolean touchMoved(int x, int y) {
		// TODO Auto-generated method stub
		return false;
	}


	@Override
	public boolean scrolled(int amount) {
		// TODO Auto-generated method stub
		return false;
	}

}
